package com.weitu.permission.dictionaryType.controller;

import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.dictionaryType.entity.DictionaryType;
import com.weitu.permission.dictionaryType.service.DictionaryTypeService;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/3/14.
 */
@Controller
@RequestMapping(value = "permission/dictionaryType")
public class DictionaryTypeController extends BaseController {
    private static final String MODULE_BATH_PATH=BASE_JSP_PATH+"permission/dictionaryType";

    private final DictionaryTypeService dictionaryTypeService;

    @Autowired
    public DictionaryTypeController(DictionaryTypeService dictionaryTypeService){
        this.dictionaryTypeService=dictionaryTypeService;
    }

    /**
     * 跳转到字典类型页面
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return MODULE_BATH_PATH + "/list";
    }

    /**
     * 查所有列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        int count = this.dictionaryTypeService.queryCount(paramMap);//获取列表总数量

        DataGridPagination dataGridPagination = DataGridPagination.getInstance(request, count);

        PageView pageView = dataGridPagination.getPageView();//生成pageView分页对象
        //未传sort参数是采用添加时间排序
        if (paramMap.get("sort") == null){
            pageView.setSortColumn("addTime");
        }

        paramMap.put("pageView", pageView);

        List<DictionaryType> dataList = this.dictionaryTypeService.queryDictionaryTypeListPage(paramMap);//获取列表数据

        return dataGridPagination.flushDataGridData(dataList);
    }

    /**
     * 查询全体字典类型（不分页）
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "listAll",method = RequestMethod.POST)
    public Object listAll(){
        List<DictionaryType> dataList = this.dictionaryTypeService.queryDictionaryTypeList();
        return dataList;
    }

/**
 * 根据名称查列表
 */
    /**
     * 字典类型添加弹框
     * @return
     */
    @RequestMapping(value = "/addDictionary", method = RequestMethod.GET)
    public String add() {
        return MODULE_BATH_PATH + "/add";
    }

    /**
     * 添加字典类型
     * @param dictionaryType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addDictionary", method = RequestMethod.POST)
    @SystemLog(module = "字典类型管理",methods = "添加字典类型")
    public AjaxResult addAjax(DictionaryType dictionaryType) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        AjaxResult result = new AjaxResult();

        putParametersIntoMap(paramMap);

        dictionaryType.setId(IDGenerator.getId());
        dictionaryType.setAddTime(DateTimeUtil.formatterDateTime());
        dictionaryType.setAddUserId(BaseController.getSessionUser().getId());
        dictionaryType.setDelState(0);
        int count = this.dictionaryTypeService.insertDictionaryType(dictionaryType);//受影响的行数

        if (count>0){
            result.setMessage("保存成功！");
            result.setSuccess(true);
            return result;
        }else {
            result.setMessage("保存失败！");
            result.setSuccess(false);
            return result;
        }
    }

    /**
     * 字典类型编辑弹框
     * @return
     */
    @RequestMapping(value = "/editDictionaryType",method = RequestMethod.GET)
    public ModelAndView editDictionary(String id) {

        DictionaryType dictionaryType=dictionaryTypeService.queryDictionaryTypeByID(id);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("dictionaryType", dictionaryType);
        modelAndView.setViewName(MODULE_BATH_PATH + "/edit");

        return modelAndView;
    }
    /**
     * 编辑字典类型
     * @param dictionaryType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editDictionaryType",method = RequestMethod.POST)
    @SystemLog(module = "字典类型管理",methods = "编辑字典类型")
    public AjaxResult editDictionaryAjax(DictionaryType dictionaryType) {
        Map<String, Object> paramMap = new HashMap<String, Object>();

        putParametersIntoMap(paramMap);
        dictionaryType.setUpdateTime(DateTimeUtil.formatterDateTime());
        dictionaryType.setUpdateUserId(BaseController.getSessionUser().getId());
        int count = this.dictionaryTypeService.updateDictionaryType(dictionaryType);//受影响的行数
        AjaxResult result = new AjaxResult();
        if (count>0){
            result.setMessage("保存成功！");
            result.setSuccess(true);
            return result;
        }else {
            result.setMessage("保存失败！");
            result.setSuccess(false);
            return result;
        }
    }
    /**
     * 删除字典类型
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteDictionaryType",method = RequestMethod.POST)
    @SystemLog(module = "字典类型管理",methods = "删除字典类型")
    public AjaxResult deleteDictionaryAjax(String id){
        int boo=dictionaryTypeService.deleteDictionaryType(id);
        AjaxResult result = new AjaxResult();
        if(boo>=1){
            result.setMessage("操作成功");
            result.setSuccess(true);
            return result;
        }else {
            result.setMessage("操作失败！");
            result.setSuccess(false);
            return result;
        }

    }

    /**
     * 添加编辑判断code
     * @param code
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addAndAlterDictionaryTypeJudgement",method = RequestMethod.POST)
    public AjaxResult addAndAlterDictionaryTypeJudgement(String code){
        int count=this.dictionaryTypeService.queryCountByCode(code);
        AjaxResult result = new AjaxResult();
        if (count>0){
            result.setMessage("标识重复，保存失败！");
            result.setSuccess(false);
            return result;
        }else {
            result.setMessage("保存成功！");
            result.setSuccess(true);
            return result;
        }
    }
    /**
     * 获取model弹框
     * @return 弹框
     */
    @RequestMapping(value = "/add/list", method = RequestMethod.GET)
    public String addModel(){
        return MODULE_BATH_PATH+"/add/list";
    }
}
